#
#2008 Julka Lipkova
#

import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import gobject

from linebackground import LineBackground
from drawingarea import MyDrawingArea
from glyphdb import glyphDB
from wholeglyphbg import WholeGlyphBg
from checker import Checker
from glyph import Glyph
import config

class ExamineTask ( gobject.GObject):
    def __init__(self):
        gobject.GObject.__init__( self )
        self.editingArea = MyDrawingArea()
        self.glade = gtk.glade.XML( config.gladeFile( 'tryIt.glade' ) )
        self.editorWidget = self.glade.get_widget( 'tryItMain' )
        self.editorWidget.parent.remove( self.editorWidget )
        self.glade.get_widget( 'vboxDrawingArea' ).pack_start( self.editingArea )
        self.lineBg = LineBackground()
        self.actualID = glyphDB.getRandomID()
        self.wholeGlyphBg = WholeGlyphBg( self.actualID )
        self.checker = None

        self.glade.signal_autoconnect( self )
        self.editingArea.connect( 'scribble_finished', self.on_scribble_finished );
        self.editingArea.addBg( self.lineBg )
        self.editingArea.addBg( self.wholeGlyphBg )

    def getMainWidget(self):
        return self.editorWidget

    def on_scribble_finished(self, sender):
        g = Glyph()
        g.lines = self.editingArea.lineList
        sbinfo = ''
        if ( len( g.lines ) == 0):
            print 'Nothing to be checked'
            sbinfo = 'Nothing to be checked'
        else:
            print 'Checking properties'
            if ( self.checker is not None ):
                self.checker = None
            self.checker = Checker( self.actualID )
            res = self.checker.check( g )
            if len(res)==0:
                sbinfo='OK!'
            else:
                sbinfo = reduce( lambda x,y: x+' '+y, res )

        self.emit( 'statusbar_notify', sbinfo )
# TODO: add old glyph as background into editingArea
        self.editingArea.start_scribble()

    def on_btnClear_clicked(self, sender):
        sbinfo = ''
        self.emit( 'statusbar_notify', sbinfo )
        self.editingArea.clear()

    def on_btnNew_clicked(self, sender):
        sbinfo = ''
        self.emit( 'statusbar_notify', sbinfo )
        self.actualID = glyphDB.getRandomID()
        self.editingArea.removeBg( self.wholeGlyphBg )
        del self.wholeGlyphBg
        self.wholeGlyphBg = WholeGlyphBg( self.actualID )
        self.editingArea.addBg( self.wholeGlyphBg )
# TODO: remove old glyph background
        self.editingArea.clear()



gobject.signal_new( 'statusbar_notify', ExamineTask,
        gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,) )


# vim: set ts=4 sw=4 sts=4 et :
